package com.xone.xapp;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class UserInfoItemActivity extends BasicActivity {
	protected LinearLayout outerView;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		outerView = (LinearLayout)findViewById(R.id.user_info_item_details_zone);
		Button buttonUpdate = (Button)findViewById(R.id.user_info_update);
		buttonUpdate.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(UserInfoItemActivity.this, UserInfoUpdateActivity.class);
				intent.putExtra("id", UserInfo.userMap.get("username"));
				startActivity(intent);
			}
		});
	}
	
	public void onResume() {
		super.onResume();
		showProgress();
		new InitDataTask(this, outerView).execute(UserInfo.userMap.get("username"));
	}
	
	@Override
	public String getHeaderTitle() {
		return "用户信息";
	}

	@Override
	public int getContentView() {
		return R.layout.user_info_item;
	}
	
	public class InitDataTask extends AsyncTask<String, Integer, JSONObject> {
		protected Context context;
		protected LinearLayout view;
		public InitDataTask(Context context, LinearLayout view) {
			this.context = context;
			this.view = view;
		}
		@Override
		protected JSONObject doInBackground(final String... params) {
			if (isCancelled()) {
				return null;
			}
			List<NameValuePair> reqparams = new ArrayList<NameValuePair>();
			reqparams.add(new BasicNameValuePair("person.username", params[0]));
			return HttpUtils.httpGet(Constants.APP_URL + "/login/indexUpdateJson.html", reqparams, 
					new HttpUtils.onGetSuccessListener<JSONObject>() {
						@Override
						public JSONObject onSuccess(int status, String result) {
							if (HttpUtils.SC_OK == status) {
								JSONObject jsonObject = JSONUtils.getJSONObjectFromString(result);
								JSONObject person = JSONUtils.getJSONObjectForKey(jsonObject, "person");
								return person;
							} else {
								Toast.makeText(context, "Status Code:" + status, Toast.LENGTH_SHORT).show();
							}
							return null;
						}
					}, context);
		}
		@Override
		protected void onPostExecute(JSONObject result) {
			super.onPostExecute(result);
			if (null == result) {
				for (Map.Entry<String, String> entity : UserInfo.userMap.entrySet()) {
					int resId = getResources().getIdentifier(entity.getKey(), "id", getPackageName());
					TextView textView = (TextView)findViewById(resId);
					if (null != textView) {
						String value = entity.getValue();
						if (StringUtils.isNotBlank(value)) {
							textView.setText(value);
						}
					}
				}
				hideProgress();
				return;
			}
			JSONObject person = result;
			int childCount = view.getChildCount();
			for (int i = 0 ; i < childCount; i++) {
				View v = view.getChildAt(i);
				String tag = (String)v.getTag();
				if (v instanceof LinearLayout && null != tag) {
					String value = null;
					if (!person.isNull(tag)) {
						value = JSONUtils.getStringFromJSONObject(person, tag);
					}
					if (null != value) {
						TextView tv = (TextView)view.findViewById(context.getResources().getIdentifier(tag, "id", context.getPackageName()));
						if (null != tv) {
							tv.setText(value);
						}
					}
				}
			}
			hideProgress();
		}
	}

}
